Skip to content

Fix #15031 (False positive: knownConditionTrueFalse reported when calling a function with a known result) - #8853

Open
danmar wants to merge 3 commits into
cppcheck-opensource:mainfrom
cppchecksolutions:fix-15031
Open

danmar wants to merge 3 commits into
cppcheck-opensource:mainfrom
cppchecksolutions:fix-15031

Conversation

@danmar

@danmar danmar commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

No description provided.

Comment thread test/testcondition.cpp
" return false;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6:12] -> [test.cpp:7:21]: (style) Assigned value 's.g()' is always true [knownConditionTrueFalse]\n", errout_str());
TODO_ASSERT_EQUALS("[test.cpp:6:12] -> [test.cpp:7:21]: (style) Assigned value 's.g()' is always true [knownConditionTrueFalse]\n", "", errout_str());

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrchr-github you added this testcase (c4f754e). therefore I wonder if you can review my fix.
I can understand that we warn here in your test but not on all boolean assignments with known result from some function call. In your test the function call is used in a condition..

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can preserve a useful warning by checking for the symbolic value here (warn if a known function result was assigned):
https://github.com/cppchecksolutions/cppcheck/blob/cc71140c09cc842cf6d4c917d9b7fa32ee82bc09/lib/checkcondition.cpp#L1574

@danmar danmar Sep 14, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm..

I believe the original motivation for knownConditionTrueFalse was to flag that there is some dead code path so it would be possible to just remove certain code. If the condition was always true we could remove the condition. If the condition was always false we could remove the whole conditional body.

then we wanted to write warnings for this: if (foo()) flag |= foo(); .. the condition itself might be true or false but it's redundant. the code can be written as flag |= foo();.

I have the feeling that this warning has a different purpose. we would not recommend to remove the condition or the assignment.. it makes the code less explicit if we replace s.g() with true in the assignment..

@danmar

danmar commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator Author

I don't want to do anything drastic now.. but it feels to me like we should split out some checking after the release.

In your example code.. what is the motivation to warn about the assignment and what is the fix supposed to be?

bool f(S s) {
    if (s.g()) {
        bool b = s.g();
        return b;
    }
    return false;
}

We don't check how the assigned variable is used after the assignment right?

@chrchr-github

Copy link
Copy Markdown
Collaborator

I don't want to do anything drastic now.. but it feels to me like we should split out some checking after the release.

In your example code.. what is the motivation to warn about the assignment and what is the fix supposed to be?

bool f(S s) {
    if (s.g()) {
        bool b = s.g();
        return b;
    }
    return false;
}

We don't check how the assigned variable is used after the assignment right?

The motivation is in https://trac.cppcheck.net/ticket/14392
My idea above would move the warning from the assignment to the return statement.

@danmar

danmar commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator Author

My idea above would move the warning from the assignment to the return statement.

ok I am still not sure what is the motivation to warn about the return statement? It's not bad to return true from a function. It might be more explicit to write s.g() in the code than true to indicate what the returned value comes from?

I would say that my motivation and description in the doc I wrote does not mean that there should be a warning for the return statement and somehow we should write a motivation and description that match the return statement warnings.

@danmar

danmar commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator Author

currently the checker does not care about what happens after the assignment. We get a warning here too:

struct S {
        bool g() const { return m; }
        bool m{};
    };

bool f(S s) {
    if (s.g()) {
        bool b = s.g(); // <- Assigned value 's.g()' is always true [knownConditionTrueFalse]
        foo(s,&b);
    }
    return false;
}

I assume your suggestion is that we do not warn here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants